<?php

class YahooComponent extends Object {
	function getResults($cat, $loc, $max_results = 0) {
		$db = new DATABASE_CONFIG();
		$db_host = $db->default['host'];
		$db_user = $db->default['login'];
		$db_pass = $db->default['password'];
		$db_name = $db->default['database'];
		
		$db = mysql_connect($db_host, $db_user, $db_pass);
		mysql_select_db($db_name, $db);
		
		$q = mysql_query(
			"SELECT html FROM yahoo_cache ".
			"WHERE ".
				"cat_id = '".mysql_real_escape_string($cat,$db)."' AND ".
				"loc_id = '".mysql_real_escape_string($loc,$db)."'",
			$db
		);
		if (mysql_num_rows($q) == 0) {
			$q = mysql_query("SELECT name FROM categories WHERE id = '".mysql_real_escape_string($cat,$db)."'");
			if (mysql_num_rows($q) != 1) {
				$looking_for = '';
			} else {
				$row = mysql_fetch_assoc($q);
				$looking_for = $row['name'].' varna ';
			}
			
			$q = mysql_query("SELECT name FROM locations WHERE id = '".mysql_real_escape_string($loc,$db)."'");
			if (mysql_num_rows($q) != 1) {
				$location = '';
			} else {
				$row = mysql_fetch_assoc($q);
				$location = $row['name'];
			}
			
			$looking_for = urlencode($looking_for);
			$location = urlencode($location);
			
			if (strlen($looking_for) > 0 && strlen($location) > 0) {
				$search_query = "{$looking_for}+in+{$location}";
			} else {
				$search_query = "{$looking_for}{$location}";
			}
			
			$ch = curl_init("http://www.yahoo.com/");
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
			curl_setopt($ch, CURLOPT_COOKIE, "DNR=1");
			$html = curl_exec($ch);
			
			if (!preg_match('#<form[^>]*action="([^"]+)"#is', $html, $m)) {
				return 'No search form';
			}
			$search_url = $m[1];
			
			if (!preg_match('#(<input[^>]*name="fr"[^>]*>)#is', $html, $m)) {
				return 'No fr';
			}
			$fr_html = $m[1];
			if (!preg_match('#value="([^"]*)"#is', $fr_html, $m)) {
				return 'No fr value';
			}
			$fr = $m[1];
			
			curl_setopt(
				$ch,
				CURLOPT_URL,
				"{$search_url}?p={$search_query}&toggle=1&cop=mss&ei=windows-1251&fr={$fr}"
			);
			$html = curl_exec($ch);
			
			$html = preg_replace('#<head[^>]*>.*?</head>#is', '', $html);
			$html = preg_replace('#<script[^>]*>.*?</script>#is', '', $html);
			$html = preg_replace('#<form[^>]*>.*?</form>#is', '', $html);
			$html = preg_replace('#<!DOCTYPE[^>]*>#is', '', $html);
			$html = preg_replace('#<html[^>]*>#is', '', $html);
			$html = preg_replace('#<body[^>]*>#is', '', $html);
			$html = preg_replace('#</body>#is', '', $html);
			$html = preg_replace('#</html>#is', '', $html);
			
			if (preg_match('#<ol[^>]*data-bns="API"[^>]*>(.*?)</ol>#is', $html, $m)) $html = $m[1];
			
			$html = preg_replace('#(\\s*-\\s*)?<a[^>]*>Cached</a>#is', '', $html);
			$html = preg_replace('#<a[^>]*>#is', '', $html);
			
			$html = preg_replace('#>\\s*<#is', ">\n<", $html);
// 			file_put_contents('/var/www/localhost/htdocs/ys/cakeapp/views/inits/t1.html', $html);
			
			mysql_query(
				"INSERT INTO yahoo_cache SET ".
					"cat_id = '".mysql_real_escape_string($cat,$db)."',".
					"loc_id = '".mysql_real_escape_string($loc,$db)."',".
					"html = '".mysql_real_escape_string($html,$db)."'",
				$db
			);
		} else {
			$row = mysql_fetch_assoc($q);
			$html = $row['html'];
		}
		
		if ($max_results != 0) {
			$max_results++;
		  $xResults = preg_split('#<div[^>]*class="res"[^>]*>#is', $html, -1, PREG_SPLIT_NO_EMPTY);
		  $numResults = count($xResults);
		  if ($max_results < $numResults) {
		    $join_array = array();
		    for ($i = 0; $i < $max_results; $i++) $join_array[] = $xResults[$i];
		    $html = join($join_array, '<div class="res">');
		  }
		}
		
		return $html;
	}
}